//  Parameter details refer to:https://support.huaweicloud.com/api-vpc/vpc_api01_0001.html
resource "huaweicloud_vpc" "vpc" {
  name        = var.vpc_name
  cidr        = var.vpc_cidr
  description = "Creaye By ResourceFormation, please don't delete manually."
  enterprise_project_id = var.enterprise_project_id
}

resource "huaweicloud_vpc_subnet" "subnet" {
  vpc_id            = huaweicloud_vpc.vpc.id
  name              = var.name_suffix != null ? format("%s-%s", var.subnet_name, var.name_suffix) : var.subnet_name
  cidr              = var.vpc_subnet_cidr
  gateway_ip        = var.vpc_subnet_gateway_ip
  dns_list          = var.vpc_subnet_dns_list
  availability_zone = var.availability_zone
  
  tags = merge(
    { "Name" = var.name_suffix != null ? format("%s-%s", var.subnet_name, var.name_suffix) : var.subnet_name },
    var.subnet_tags)
}

resource "huaweicloud_networking_secgroup" "secgroup" {
  name                 = var.secgroup_name
  description          = "My ECS security group"
  delete_default_rules = true
}

resource "huaweicloud_networking_secgroup_rule" "allow_ports" {
  count = var.allow_ports_configuration != null ? length(var.allow_ports_configuration) : 0
  
  security_group_id = huaweicloud_networking_secgroup.secgroup.id
  description      = lookup(element(var.allow_ports_configuration, count.index), "description")
  direction        = lookup(element(var.allow_ports_configuration, count.index), "direction")
  ethertype        = lookup(element(var.allow_ports_configuration, count.index), "ethertype")
  protocol         = lookup(element(var.allow_ports_configuration, count.index), "protocol")
  ports            = lookup(element(var.allow_ports_configuration, count.index), "ports")
  remote_ip_prefix = lookup(element(var.allow_ports_configuration, count.index), "remote_ip_prefix")
}

//  Configure the ECS outbound security group and modify it according to the actual situation
resource "huaweicloud_networking_secgroup" "rds_secgroup" {
  count                = var.is_rds_secgroup_creat ? 1 :0
  name                 = var.rds_secgroup_name
  description          = "My RDS security group"
  delete_default_rules = true
}

resource "huaweicloud_networking_secgroup_rule" "rds_ingress_allow_ports" {
  
  count                   = var.is_rds_secgroup_creat && var.rds_ingress_allow_ports_configuration != null ? length(var.rds_ingress_allow_ports_configuration ) : 0
  security_group_id       = huaweicloud_networking_secgroup.rds_secgroup[0].id
  description      = lookup(element(var.allow_ports_configuration, count.index), "description")
  direction        = lookup(element(var.allow_ports_configuration, count.index), "direction")
  ethertype        = lookup(element(var.allow_ports_configuration, count.index), "ethertype")
  protocol         = lookup(element(var.allow_ports_configuration, count.index), "protocol")
  ports            = lookup(element(var.allow_ports_configuration, count.index), "ports")
  remote_ip_prefix = lookup(element(var.allow_ports_configuration, count.index), "remote_ip_prefix")
}
